﻿// ==PREPROCESSOR==
// @import "%fb2k_path%scripts\marc2003\v2\common.js"
// ==/PREPROCESSOR==

var panel_id = window.GetProperty("panel_id", window.id);
var cycle_tf_file = settings_path + panel_id + "cycle";
var cycle_tf = read(cycle_tf_file);

var g_text, g_img, new_width, new_height;

var g_tooltip = window.CreateTooltip();
var hover = null;

var image_set = null;
var art_image_path = null;
var index = 0;
var arr = 0;

window.GetProperty("aspect",1);
window.GetProperty("id",0);
window.GetProperty("mode", 101);
var cycle = window.GetProperty("cycle", 200) - 200;
var limit = window.GetProperty("limit", 305) - 300;

on_item_focus_change();

function on_size() {
	ww = window.Width;
	wh = window.Height;
}

function on_paint(gr) {
	gr.FillGradRect(0, 0, ww, wh, 0, RGB(22, 51, 89), RGB(242, 242, 242));
	if(g_text) {
		gr.GdiDrawText(g_text, g_font, g_textcolor, 0, 0, ww, wh, DT_VCENTER | DT_CENTER | DT_WORDBREAK | DT_CALCRECT | DT_NOPREFIX);
	} else if (g_img) {
		if(window.GetProperty("aspect") == 1) {
			if(g_img.Width < ww && g_img.Height < wh && window.GetProperty("mode") != 101) {
				new_width = g_img.Width;
				new_height = g_img.Height;
				pos_x = (ww - g_img.Width) / 2;
				pos_y = (wh - g_img.Height) /2;
			} else {
				scale = Math.min(ww / g_img.Width, wh / g_img.Height);
				new_width = g_img.Width * scale;
				new_height = g_img.Height * scale;
				pos_x = (ww - new_width) / 2;
				pos_y = (wh - new_height) / 2;
			}
		} else {
			new_width = ww;
			new_height = wh;
			pos_x = 0;
			pos_y = 0;
		}
		gr.DrawImage(g_img, pos_x, pos_y, new_width, new_height, 0, 0, g_img.Width, g_img.Height);
	} else {
		gr.GdiDrawText(!g_metadb ? "[no selection]" : "[no image]", g_font, g_textcolor, 0, 0, ww, wh, DT_VCENTER | DT_CENTER | DT_WORDBREAK | DT_CALCRECT | DT_NOPREFIX);
	}
}

function on_playback_new_track() {
	image_set = null;
	on_item_focus_change();
}

function on_metadb_changed() {
	g_text = null;
	if(window.GetProperty("mode") == 101) {
		if(g_img) g_img.Dispose();
		g_img = null;
		type = window.GetProperty("id");
		g_img = utils.GetAlbumArtEmbedded(g_metadb.rawpath, type);
		if(g_img) {
			is_embedded = "Yes";
			window.Repaint();
		} else {
			is_embedded = "No";
			utils.GetAlbumArtAsync(window.ID, g_metadb, type);
		}
	} else {
		if(!fb.IsPlaying) image_set = null;
		if(image_set) return;
		if(window.GetProperty("mode") == 102) {
			artist = fb.TitleFormat("%artist%").EvalWithMetadb(g_metadb);
			folder = data_folder + fb.TitleFormat("$crc32(%artist%)").EvalWithMetadb(g_metadb);
		} else {
			folder = fb.titleformat(cycle_tf).EvalWithMetadb(g_metadb);
		}
		var jpg = utils.Glob(folder + "\\*.jpg").toArray();
		var png = utils.Glob(folder + "\\*.png").toArray();
		var gif = utils.Glob(folder + "\\*.gif").toArray();
		arr = jpg.concat(png, gif);
		index = Math.floor(Math.random()*arr.length)
		load_image();
	}
}

function on_get_album_art_done(metadb, art_id, im, ip) {
	art_image_path = ip;
	g_img = im;
	window.Repaint();
}

function load_image() {
	image_set = true;
	art_image_path = arr[index];
	if(g_img) g_img.Dispose();
	g_img = gdi.Image(art_image_path);
	window.Repaint();
}

function on_playback_time(time) {
	if(window.GetProperty("mode") != 101 && cycle > 0 && arr.length > 1 && (time % cycle == 0)) on_mouse_wheel(1);
}

function on_mouse_move(x,y) {
	if(!hover && g_img) {
		embedded = window.GetProperty("mode") == 101 ? "Embedded image: " + is_embedded + ". " : '';
		g_tooltip.Text = embedded + "Actual size: " + g_img.Width + " x " + g_img.Height + ". Current size: " + Math.round(new_width) + " x " + Math.round(new_height) + ".";
		g_tooltip.Activate();
		hover = true;
	}
}

function on_mouse_leave() {
	g_tooltip.Deactivate();
	hover = null;
}

function on_mouse_wheel(delta) {
	if(arr.length < 2 || window.GetProperty("mode") == 101) return;
	index += delta;
	if(index < 0) index = arr.length-1;
	if(index == arr.length) index = 0;
	load_image();
	g_tooltip.Deactivate();
}
	
function on_mouse_lbtn_dblclk() {
	if(!g_metadb || !g_img) return;
	if(window.GetProperty("mode") == 101 && is_embedded == "Yes") {
		fb.RunContextCommandWithMetadb("Open Containing Folder", g_metadb);
	} else {
		try {WshShell.Run("\"" + art_image_path + "\""); } catch(e) {}
	}
}

function on_mouse_rbtn_up(x, y) {
	var _menu = window.CreatePopupMenu();
	var _child = window.CreatePopupMenu();
	var _cycle = window.CreatePopupMenu();
	var _limit = window.CreatePopupMenu();
	var idx;
	_child.AppendMenuItem(MF_STRING, 101, "Artreader");
	_child.AppendMenuItem(MF_STRING, 102, "Last.fm Artist Art");
	_child.AppendMenuItem(MF_STRING, 103, "Cycle Folder");
	_child.CheckMenuRadioItem(101, 103, window.GetProperty("mode"));
	_cycle.AppendMenuItem(MF_STRING, 200, "Off");
	_cycle.AppendMenuItem(MF_STRING, 205, "5 seconds");
	_cycle.AppendMenuItem(MF_STRING, 210, "10 seconds");
	_cycle.AppendMenuItem(MF_STRING, 220, "20 seconds");
	_cycle.CheckMenuRadioItem(200, 220, window.GetProperty("cycle"));
	_limit.AppendMenuItem(MF_STRING, 301, "1");
	_limit.AppendMenuItem(MF_STRING, 303, "3");
	_limit.AppendMenuItem(MF_STRING, 305, "5");
	_limit.AppendMenuItem(MF_STRING, 310, "10");
	_limit.CheckMenuRadioItem(301, 310, window.GetProperty("limit"));
	if(window.GetProperty("mode") == 101) {
		_menu.AppendMenuItem(MF_STRING, 1, "Refresh");
		_menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
		_menu.AppendMenuItem(MF_STRING, 2, "Front Cover");
		_menu.AppendMenuItem(MF_STRING, 3, "Back Cover");
		_menu.AppendMenuItem(MF_STRING, 4, "Disc");
		_menu.AppendMenuItem(MF_STRING, 5, "Icon");
		_menu.AppendMenuItem(MF_STRING, 6, "Artist");
	}
	if(window.GetProperty("mode") == 102) {
		_menu.AppendMenuItem(g_metadb ? MF_STRING : MF_GRAYED, 11, "Download artist art from Last.fm");
		_menu.AppendMenuItem(MF_STRING | MF_POPUP, _limit.ID, "Limit");
		_menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
	}
	if(window.GetProperty("mode") != 101) {
		_menu.AppendMenuItem(g_metadb && fso.FolderExists(folder) ? MF_STRING : MF_GRAYED, 12, "Open containing folder");
		_menu.AppendMenuItem(MF_STRING | MF_POPUP, _cycle.ID, "Cycle");
	}
		if(window.GetProperty("mode") == 103) {
		_menu.AppendMenuItem(g_metadb ? MF_STRING : MF_GRAYED, 21, "Set Cycle Folder....");
	}
	_menu.AppendMenuItem(MF_SEPARATOR, 0, 0);
	_menu.AppendMenuItem(MF_STRING, 97, "Correct Aspect Ratio");
	_menu.AppendMenuItem(MF_STRING | MF_POPUP, _child.ID, "Mode");
	//_menu.AppendMenuItem(MF_STRING, 99, "Properties");
	_menu.AppendMenuItem(MF_STRING, 100, "Configure...");
	_menu.CheckMenuRadioItem(2, 6, window.GetProperty("id")+2);
	_menu.CheckMenuItem(97, window.GetProperty("aspect"));
	idx = _menu.TrackPopupMenu(x, y);
	switch(idx) {
		case 101:
		case 102:
		case 103:
			window.SetProperty("mode", idx);
			image_set = null;
			art_image_path = null;
			g_img = null;
			window.Repaint();
			on_metadb_changed();
			break;
		case 200:
		case 205:
		case 210:
		case 220:
			window.SetProperty("cycle", idx);
			cycle = idx - 200;
			break;
		case 301:
		case 303:
		case 305:
		case 310:
			window.SetProperty("limit", idx);
			limit = idx - 300;
			break;
		case 1:
			on_metadb_changed();
			break;
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
			window.SetProperty("id", idx-2);
			on_metadb_changed();
			break;
		case 11:
			g_text = "PLEASE WAIT.....";
			window.Repaint();
			WshShell.Run("\"" + script_path + "art.exe\" \""  + artist + "\" \"" + folder + "\" " + limit, 0, true);
			g_text = null;
			image_set = null;
			on_metadb_changed();
			break;
		case 12:
			WshShell.Run("explorer /select," + art_image_path);
			break; 
		case 21:
			cycle_tf = text_input_box("WSH Art Panel", "Enter title formatting or an absolute path to a folder.", cycle_tf_file);
			image_set = null;
			artist_image_path = null;
			 g_img = null;
			on_metadb_changed();
			break;
		case 97:
			window.GetProperty("aspect") == 0 ? window.SetProperty("aspect", 1) : window.SetProperty("aspect", 0);
			window.Repaint();
			break;
		case 99:
			window.ShowProperties();
			break;
		case 100:
			window.ShowConfigure();
			break;
	}
	_menu.Dispose();
	_child.Dispose();
	_cycle.Dispose();
	_limit.Dispose();
	return true;
}